﻿using Newtonsoft.Json;
using System;

namespace JsonTest
{
    public class JsonDatetimeConverter : JsonConverter<DateTime>
    {
        public override DateTime ReadJson(JsonReader reader, Type objectType, DateTime existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            return JsonConvert.DeserializeObject<DateTime>($"\"{(string)reader.Value}\"");
        }

        public override void WriteJson(JsonWriter writer, DateTime value, JsonSerializer serializer)
        {
            //var nd = value.AddMilliseconds(-value.Millisecond);
            //writer.WriteValue(JsonConvert.SerializeObject(nd));
            writer.WriteValue($"{value:G}");
        }
    }
}
